/**************************************************************
 *@ProjectName: blweibo
 *@FileName: Message.java
 *@Date: 2011-5-30
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo.models;

import java.io.Serializable;

import org.json.JSONObject;

import com.zhiweibo.util.TopicUtil;

/**
 * The Class Message.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-30
 */
public class Message implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 4001074014828150255L;

    /** The id. */
    private String id = "";

    /** The text. */
    private String text = "";

    /** The fromuid. */
    private String fromuid = "";

    /** The touid. */
    private String touid = "";

    /** The dateline. */
    private String dateline = "";

    /** The sender. */
    private User sender;

    /** The recipient. */
    private User recipient;

    /**
     * Instantiates a new message.
     */
    public Message() {
    }

    /**
     * Instantiates a new message.
     * 
     * @param js
     *            the js
     */
    public Message(JSONObject js) {
        if (js == null) {
            return;
        }
        id = js.optString("id");
        text = js.optString("text");
        fromuid = js.optString("fromuid");
        touid = js.optString("touid");
        dateline = js.optString("dateline");
        sender = new User(js.optJSONObject("sender"));
        recipient = new User(js.optJSONObject("recipient"));
    }

    /**
     * Gets the id.
     * 
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * Sets the id.
     * 
     * @param id
     *            the new id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Gets the text.
     * 
     * @return the text
     */
    public String getText() {
        return text;
    }

    /**
     * Sets the text.
     * 
     * @param text
     *            the new text
     */
    public void setText(String text) {
        this.text = text;
    }

    /**
     * Gets the fromuid.
     * 
     * @return the fromuid
     */
    public String getFromuid() {
        return fromuid;
    }

    /**
     * Sets the fromuid.
     * 
     * @param fromuid
     *            the new fromuid
     */
    public void setFromuid(String fromuid) {
        this.fromuid = fromuid;
    }

    /**
     * Gets the touid.
     * 
     * @return the touid
     */
    public String getTouid() {
        return touid;
    }

    /**
     * Sets the touid.
     * 
     * @param touid
     *            the new touid
     */
    public void setTouid(String touid) {
        this.touid = touid;
    }

    /**
     * Gets the dateline.
     * 
     * @return the dateline
     */
    public String getDateline() {
        return TopicUtil.formatDate(dateline);
    }

    /**
     * Sets the dateline.
     * 
     * @param dateline
     *            the new dateline
     */
    public void setDateline(String dateline) {
        this.dateline = dateline;
    }

    /**
     * Gets the sender.
     * 
     * @return the sender
     */
    public User getSender() {
        return sender;
    }

    /**
     * Sets the sender.
     * 
     * @param sender
     *            the new sender
     */
    public void setSender(User sender) {
        this.sender = sender;
    }

    /**
     * Gets the recipient.
     * 
     * @return the recipient
     */
    public User getRecipient() {
        return recipient;
    }

    /**
     * Sets the recipient.
     * 
     * @param recipient
     *            the new recipient
     */
    public void setRecipient(User recipient) {
        this.recipient = recipient;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Message [id=" + id + ", text=" + text + ", fromuid=" + fromuid + ", touid=" + touid + ", dateline="
                + dateline + ", sender=" + sender + ", recipient=" + recipient + "]";
    }

    /**
     * To json string.
     * 
     * @return the string
     */
    public String toJSONString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"id\"").append(":\"").append(id).append("\",");
        sb.append("\"text\"").append(":\"").append(text).append("\",");
        sb.append("\"fromuid\"").append(":\"").append(fromuid).append("\",");
        sb.append("\"touid\"").append(":\"").append(touid).append("\",");
        sb.append("\"dateline\"").append(":\"").append(dateline).append("\",");
        sb.append("\"sender\"").append(":").append(sender.toJSONString()).append(",");
        sb.append("\"recipient\"").append(":").append(recipient.toJSONString());
        sb.append("}");
        return sb.toString();
    }
}
